import { RouteRecordRaw } from 'vue-router';

export const transformMenuToRoutes = (menus: API.MenuItem[]) => {
  return menus.map(menu => {
    const { path, component, children, meta } = menu as any;
    const route: RouteRecordRaw = {
      path,
      component: () => import(`@/views/${component}/index.vue`),
      meta: {
        title: meta?.title,
        icon: meta?.icon
      }
    } as RouteRecordRaw;
    if (children) {
      route.children = transformMenuToRoutes(children);
    }
    return route;
  });
};

export const generateDynamicRoutes = (menus: API.MenuItem[]) => {
  const routes = [...transformMenuToRoutes(menus)];

  return routes;
};
